public class PalindromeLinkedList {
    public boolean isPalindrome(ListNode head) {
        ListNode slow = head, fast = head;
        ListNode virtualNode = new ListNode();

        // 找到中间节点
        while(fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }

        // 头插法逆序后半部分链表
        while(slow != null) {
            ListNode next = slow.next;
            slow.next = virtualNode.next;
            virtualNode.next = slow;
            slow = next;
        }

        // 判断是否回文
        ListNode cur = virtualNode.next;
        while(cur != null) {
            if(head.val != cur.val) {
                return false;
            }
            head = head.next;
            cur = cur.next;
        }
        return true;
    }
}
